package com.thinkingstar.iads.common.utils;


import java.io.Closeable;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.math.BigDecimal;
import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.servlet.http.HttpServletRequest;

import org.nutz.lang.Strings;
public class UploadFileUtil {
	
	public static final Pattern RANGE_PATTERN = Pattern.compile("bytes \\d+-\\d+/\\d+");
	
	public static final String CONTENT_RANGE_HEADER = "content-range";
	
	public static final int BUFFER_LENGTH = 10240;
	
	public static final String START_FIELD = "start";
	
	public static final String FILE_NAME_FIELD = "name";
	
	public static final String FILE_SIZE_FIELD = "size";
	
	public static final String TOKEN_FIELD = "token";
	
	public static final String SUCCESS = "success";
	
	public static final String ERROR = "error";
	
	public static final String MESSAGE = "message";
	
	public static final String DATA = "data";
	
	public static final String TYPE = "type";
	/** 公共文件  */
	public static final String COMMON = "common";
	/** 共享文档  */
	public static final String DOCUMENT = "document";
	/** 模板文件  */
	public static final String TEMPLATE = "template";
	/** 常量文件  */
	public static final String CONSTANT = "constant";
	/** 公式文件  */
	public static final String FORMULA = "formula";
	/** 设备文件  */
	public static final String EQUIPMENT = "equipment";
	/** 设备配备文件  */
	public static final String EQUIPPED = "equipped";
	/** 图纸文件  */
	public static final String DRAWING = "drawing";
	/** 附件文件  */
	public static final String ATTACHMENT = "attachment";
	/** 帮助文件  */
	public static final String HELP = "help";
	/** 关联主键  */
	public static final String RELATIIN_ID = "relationId";
	
	public static final int ERROR_FILE_RANGE_START = 409;
	
	public static final int ERROR_FILE_NOT_EXIST = 401;
	
	/** 当前系统文件路径分隔符 */
	public static final String FILE_SEPARATOR = File.separator;
    
    
    /**
     * 写入文件
     * @param req
     * @return
     * @throws IOException
     */
	public static boolean write(HttpServletRequest req,File file) {
		InputStream content  = null;
		OutputStream out = null;
		int read = 0;
		final byte[] bytes = new byte[BUFFER_LENGTH];
		try {
			content = req.getInputStream();
			out = new FileOutputStream(file, true);
			while ((read = content.read(bytes)) != -1){
				out.write(bytes, 0, read);
			}
		} catch (IOException e) {
			return false;
		}finally {
			close(out);
			close(content);
		}
    	return true;
    }
    
    /**
     * 关闭流
     * @param stream
     */
    public static void close(Closeable stream) {
        try {
            if (stream != null)
                stream.close();
        } catch (IOException e) {
        }
    }

    /**
     * 返回文件大小，单位MB
     * @param filesize
     * @param scale
     * @return
     */
    public static double getFileSize(long filesize, int scale) {
        BigDecimal bd1 = new BigDecimal(Long.toString(filesize));
        BigDecimal bd2 = new BigDecimal(Long.toString(1024));
        return bd1.divide
                (bd2, scale, BigDecimal.ROUND_HALF_UP).divide(bd2, scale, BigDecimal.ROUND_HALF_UP).doubleValue();
    }
    
}
